package com.guaoran.interview.in23.collection.map;

import java.util.concurrent.ConcurrentHashMap;


/**
 * @author : 孤傲然
 * @description : ConcurrentHash 线程安全
 * @date :2020/8/26 21:54
 */
public class ConcurrentHashMpaDemo{
    static final int NCPU = Runtime.getRuntime().availableProcessors();
    private static final int MIN_TRANSFER_STRIDE = 16;


    public static void main(String[] args) {
        ConcurrentHashMap<String,String> concurrentHashMap = new ConcurrentHashMap<>();
        for (int i = 0; i < 10000; i++) {
            concurrentHashMap.put(i+"key"+i,"v");
        }
        int rs = Integer.numberOfLeadingZeros(16) | (1 << (16 - 1));
        System.out.println(rs);//32795
        System.out.println(Integer.toBinaryString(rs));//1000000000011011
        System.out.println(rs << 16);//-2145714176
        System.out.println(Integer.toBinaryString(rs << 16));//10000000000110110000000000000000

        int n = 4096*2, stride;

        System.out.println(NCPU);
        System.out.println(Integer.toBinaryString(16));//10000
        System.out.println(n >>> 3);//2
        System.out.println((n >>> 3) / NCPU);

        for ( n = 16;n<4096*1024;n*=2){
            if ((stride = (NCPU > 1) ? (n >>> 3) / NCPU : n) < MIN_TRANSFER_STRIDE) {
                System.out.print(stride+"\t");
                stride = MIN_TRANSFER_STRIDE; // subdivide range
            }else{
                System.out.print(stride+"\t");
            }
            System.out.print(stride);
            System.out.println();
        }

        n=16;
        System.out.println(n << 1);
        System.out.println(32 + (32 >>> 1) + 1);
        System.out.println(tableSizeFor(32 + (32 >>> 1) + 1));
    }
    private static final int MAXIMUM_CAPACITY = 1 << 30;

    private static final int tableSizeFor(int c) {
        int n = c - 1;
        n |= n >>> 1;
        n |= n >>> 2;
        n |= n >>> 4;
        n |= n >>> 8;
        n |= n >>> 16;
        return (n < 0) ? 1 : (n >= MAXIMUM_CAPACITY) ? MAXIMUM_CAPACITY : n + 1;
    }
}
